package com.song.dao.impl;

import com.song.bean.Menu;
import com.song.dao.RoleDao;
import com.song.dao.Utils;
import com.song.bean.Role;


import javax.enterprise.inject.New;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends Utils implements RoleDao {
    /**
     * 查询角色数据
     *
     * @return 角色的集合
     */
    @Override
    public List<Role> getRoles(Integer pageIndex, Integer pagesize) {

        List<Role> roleList = new ArrayList<>();
        String sql = "SELECT roleid,rolename,rolestate from role limit ?,?";
        List<Object> list = new ArrayList<>();
        list.add((pageIndex - 1) * pagesize);
        list.add(pagesize);
        try {
            resultSet = select(sql, list);
            while (resultSet.next()) {
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return roleList;
    }

    /**
     * 获取信息总数
     *
     * @return 信息集合
     */
    @Override
    public List<Role> getRoles() {
        List<Role> roleList = new ArrayList<>();
        String sql = "SELECT roleid,rolename,rolestate from role ";
        try {
            resultSet = select(sql, null);
            while (resultSet.next()) {
                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleId(resultSet.getInt("roleid"));
                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return roleList;
    }

    /**
     * 新增角色
     *
     * @param roleName  新增角色的名称
     * @param menuList  新增角色的菜单
     * @param roleState 新增角色的状态
     * @return 新增角色的ID（roleid）
     */
    @Override
    public Integer insertRole(String roleName, String[] menuList, int roleState) {
        //新增角色的key(roleid)
        int key = -1;
        try {
            String sql = "INSERT INTO role values(null,?,?)";
            List<Object> list = new ArrayList<>();
            list.add(roleName);
            list.add(roleState);
            //Statement.RETURN_GENERATED_KEYS可用使pps返回新增数据的索引key
            //pps= getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            int update = update(sql, list);//返回的是影响的行数
            resultSet = pps.getGeneratedKeys();//返回新增（修改，删除）的索引
            while (resultSet.next()) {
                key = resultSet.getInt(1);//获取新增（修改，删除）的索引
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return key;
    }

    /**
     * 通过角色（roleid）查询信息
     *
     * @param roleId 角色ID
     * @return 查询角色role
     */
    @Override
    public Role findById(Integer roleId) {
        //创建角色对象
        Role role = new Role();
        //创建菜单集合
        List<Menu> menuList = new ArrayList<>();
        List<Object> list = new ArrayList<>();

        try {
            String sql = "SELECT * FROM role r,middle mid,menu m WHERE r.roleid=mid.roleid AND mid.menuid=m.menuid AND r.roleid = ?";
            list.add(roleId);
            resultSet = select(sql, list);
            while (resultSet.next()) {
                //存值角色信息
                role.setRoleId(roleId);
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                //创建菜单对象,存该对象的菜单信息
                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setFatherMenuId(resultSet.getInt("fathermenuid"));
                menuList.add(menu);
            }
            role.setMenu(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return role;
    }

    /**
     * 修改角色
     *
     * @param role     修改角色的对象
     * @param menuList 修改角色的菜单
     * @return 修改成功与否
     */
    @Override
    public Boolean updateRole(Role role, String[] menuList) {
        //角色的key(roleid)
        try {
            String sql = "update role set rolename=?, rolestate=? WHERE roleid = ?";
            List<Object> list = new ArrayList<>();
            list.add(role.getRoleName());
            list.add(role.getRoleState());
            list.add(role.getRoleId());
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

    /**
     * 通过角色ID（roleid）找到角色信息
     *
     * @param roleId 角色ID
     * @return 角色的对象
     */
    @Override
    public Role findById_update(Integer roleId) {
        Role role = new Role();
        try {
            String sql = "select * from role where roleid = ?";
            List<Object> list = new ArrayList<>();
            list.add(roleId);
            resultSet = select(sql, list);
            while (resultSet.next()) {
                role.setRoleId(roleId);
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return role;
    }
    /**
     * 删除角色通过角色ID
     * @param roleid 角色ID
     * @return true 删除成功 false 删除失败
     */
    @Override
    public Boolean deleteRole(Integer roleid) {
        try {
            String sql = "delete from role where roleid = ?";
            List<Object> list = new ArrayList<>();
            list.add(roleid);
            int update = update(sql, list);
            if (update > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return false;
    }

}
